package GUIHelper;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JTextField;

import ControlLayer.RoomBookingCtr;
import GUILayer.SaleGUI;
import ModelLayer.RoomBooking;
import ModelLayer.RoomBookingLine;

public class CheckInFrame implements ActionListener {
	private SaleGUI saleGUI;
	private JFrame frame;
	private int rbId;
	private RoomBookingCtr rbCtr;
	private RoomBooking rb;

	public CheckInFrame(JPanel saleGUI, int rbId) {
		frame = new JFrame("Check In");
		frame.setSize(300, 150);
		frame.setLocationRelativeTo(null);
		this.saleGUI = (SaleGUI) saleGUI;
		this.rbId = rbId;
		initialize();
	}

	public void initialize() {
		rbCtr = new RoomBookingCtr();
		JPanel mainPanel = new JPanel();
		frame.getContentPane().add(mainPanel);
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

		JPanel infoPanel = new JPanel();
		infoPanel.setLayout(new FlowLayout());

		// initialise id string
		ArrayList<String> ids = new ArrayList<String>();
		rb = rbCtr.getRoomBooking(rbId);
		LinkedList<RoomBookingLine> lines = rb.getLines();
		for (RoomBookingLine rbl : lines) {
			// check if any rooms hasn't checked in
			if (rbl.getRooms().size() < rbl.getQuantity())
				ids.add(Integer.toString(rbl.getRoomType().getId()));
		}

		if (ids.size() == 0) {
			JLabel lbl = new JLabel("The room booking has finished checked in");
			JLabel space1 = new JLabel(" ");
			JLabel space2 = new JLabel(" ");
			JLabel space3 = new JLabel(" ");
			JPanel btnPanel = new JPanel();
			btnPanel.setLayout(new FlowLayout());
			JButton btnOk = new JButton("OK");
			btnPanel.add(btnOk);
			mainPanel.add(space1);
			mainPanel.add(space2);
			mainPanel.add(lbl);
			mainPanel.add(space3);
			mainPanel.add(btnPanel);
			btnOk.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
				}
			});

		} else {
			final String[] rtIds = ids.toArray(new String[ids.size()]);
			final JComboBox<String> rtId = new JComboBox<String>(rtIds);
			rtId.setSelectedIndex(0);
			rtId.addActionListener(this);
			rtId.setPreferredSize(new Dimension(60, 20));

			JLabel lblrtId = new JLabel("Please select room type Id");
			JTextField textField_rtId = new JTextField();
			textField_rtId.setColumns(10);
			infoPanel.add(lblrtId);
			infoPanel.add(rtId);
			mainPanel.add(infoPanel);

			JPanel choicePanel = new JPanel();
			choicePanel.setLayout(new FlowLayout());
			JButton btnOK = new JButton("OK");
			JButton btnCancel = new JButton("Cancel");
			choicePanel.add(btnOK);
			choicePanel.add(btnCancel);

			mainPanel.add(choicePanel);

			btnOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					GuestInPutFrame guestInPutFrame = new GuestInPutFrame(
							saleGUI, Integer.parseInt(rtIds[rtId
									.getSelectedIndex()]), rb);
					guestInPutFrame.getFrame().setVisible(true);
					frame.dispose();
				}
			});

			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					frame.dispose();
				}
			});
		}

	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// JComboBox cb = (JComboBox) e.getSource();
		// String list = (String) cb.getSelectedItem();

	}

}
